package net.lab1024.sa.admin.module.business.erp.prp.service;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import net.lab1024.sa.admin.module.business.erp.prp.dao.BipordbDao;
import net.lab1024.sa.admin.module.business.erp.prp.domain.entity.BipordbEntity;
import net.lab1024.sa.admin.module.business.erp.prp.domain.form.BipordbAddForm;
import net.lab1024.sa.admin.module.business.erp.prp.domain.form.BipordbQueryForm;
import net.lab1024.sa.admin.module.business.erp.prp.domain.form.BipordbUpdateForm;
import net.lab1024.sa.admin.module.business.erp.prp.domain.vo.BipordbVO;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.domain.PageResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * BIP运算单行 Service
 *
 * @Author AKITA
 * @Date 2025-08-13 23:51:27
 * @Copyright AKITA
 */

@Service
public class BipordbService {

    @Resource
    private BipordbDao bipordbDao;

    @Resource
    private BipordService bipordService;

    public PageResult<BipordbVO> queryPage(BipordbQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<BipordbVO> list = bipordbDao.queryPage(page, queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }

    public ResponseDTO<String> add(BipordbAddForm addForm) {
        BipordbEntity bipordbEntity = SmartBeanUtil.copy(addForm, BipordbEntity.class);
        bipordbDao.insert(bipordbEntity);
        return ResponseDTO.ok();
    }




    @Transactional
    public ResponseDTO<String> update(BipordbUpdateForm updateForm) {

        if (!bipordService.checkVersion(updateForm.getId(),updateForm.getVersion())) {
            throw new RuntimeException("数据异常，刷新重试!");
        }

        BipordbEntity bipordbEntity = SmartBeanUtil.copy(updateForm, BipordbEntity.class);
        bipordbDao.update(bipordbEntity,new LambdaQueryWrapper<BipordbEntity>().eq(BipordbEntity::getId, bipordbEntity.getId()).eq(BipordbEntity::getLineNum, bipordbEntity.getLineNum()));

        if (!bipordService.upDateVesrion(updateForm.getId(), updateForm.getVersion())) {
            throw new RuntimeException("数据异常，刷新重试!");
        }
        return ResponseDTO.ok();
    }

    public ResponseDTO<String> batchDelete(List<Long> idList) {
        bipordbDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }

    public ResponseDTO<String> delete(Long id) {
        bipordbDao.deleteById(id);
        return ResponseDTO.ok();
    }

    public ResponseDTO<String> deleteByLineNum(BipordbAddForm addForm) {
        bipordbDao.delete(new LambdaQueryWrapper<BipordbEntity>().eq(BipordbEntity::getId, addForm.getId()).eq(BipordbEntity::getLineNum, addForm.getLineNum()) );
        return ResponseDTO.ok();
    }
}
